<?php

class Member_data extends Data {

	private $alias = 'www-yueshihui-com';

	private $tb = 'user';
	private $tb_emailVerify = 'email_verify';
	private $tb_online = 'online';
	private $tb_loginlog = 'loginlog';
	private $cols = array('uid', 'nickname', 'email', 'password', 'salt', 'is_active', 'created_time', 'created_ip');

	/**
	 * 创建用户
	 * @param unknown_type $cols
	 */
	public function create($cols) {
		if(!($db = $this->connect($this->alias))) return false;
		
		return $db->insert($this->tb, $cols);
	}
	/**
	 * 获取单个用户信息
	 * @param unknown_type $params
	 */
	public function userInfo($params) {
		if(!($db = $this->connect($this->alias))) return false;
		
		return $db->one($this->tb, $this->cols, $params);
	}
	/**
	 * 获取单个用户信息
	 * @param unknown_type $params
	 */
	public function userInfos($params) {
		if(!($db = $this->connect($this->alias))) return false;
		
		return $db->select($this->tb, $this->cols, $params);
	}
	/**
	 * 保存邮箱验证码
	 * @param unknown_type $cols
	 */
	public function saveEmailVerify($cols) {
		if(!($db = $this->connect($this->alias))) return false;
		
		return $db->insert($this->tb_emailVerify, $cols);
	}
	/**
	 * 获取邮箱验证码
	 * @param unknown_type $params
	 */
	public function emailVerify($params) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$user = $db->one($this->tb_emailVerify, array('id', 'email', 'verify'), $params);
	}
	/**
	 * 设置用户在线
	 * @param unknown_type $cols
	 */
	public function setOneline($cols = array()) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$result = $db->replace($this->tb_online, $cols);
		return $result;
	}
	/**
	 * 删除登陆超时用户
	 * @param unknown_type $params
	 */
	public function delOffline($params = array()) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$result = $db->delete($this->tb_online, $params);
		return $result;
	}
	/**
	 * 写登录日志
	 * @param unknown_type $cols
	 */
	public function loginLog($cols){
		if(!($db = $this->connect($this->alias))) return false;
		
		$result = $db->insert($this->tb_loginlog, $cols);
		if($result) return $db->lastInsertId();
		else return false;
	}
	/**
	 * 获取在线用户信息
	 * @param unknown_type $params
	 */
	public function onlineUser($params) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$cols = array('uid', 'last_active');
		$result = $db->one($this->tb_online, $cols, $params);
		return $result;
	}
	/**
	 * 延长在线时间
	 * @param unknown_type $uid
	 * @param unknown_type $expire_time
	 */
	public function delayOnlineTime($uid, $last_active) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$cols = array('last_active' => $last_active);
		$params = array('uid' => $uid);
		$result = $db->update($this->tb_online, $cols, $params);
		return $result;
	}
	/**
	 * 用户退出
	 * @param unknown_type $uid
	 */
	public function logout($uid) {
		if(!($db = $this->connect($this->alias))) return false;
		
		$params = array('uid' => $uid);
		$result = $db->delete($this->tb_online, $params);
		return $result;
	}
}